	/**
 * 
 */
package projeto;

import java.awt.event.FocusEvent;

import javax.swing.JOptionPane;

/**
 * Classe que corresponde a tela de cadastro do sistema, usando interface grafica.
 * @author equipe
 *
 */
public class TelaCadastro extends javax.swing.JFrame {
	
	
	private static final long serialVersionUID = 1L;
	private Sala sala;
	private Professor professor;
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextField campoMatricula;
    private javax.swing.JTextField campoBloco;
    private javax.swing.JTextField campoSala;
	
    /**
     * Cria uma tela de cadastro.
     */
    public TelaCadastro() {
        initComponents();
    }

    
    /**
     * Componentes que compoe a tela de cadastro e seus campos, onde o usuario digita seus dados.
     */
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        botaoSalvar = new javax.swing.JButton();
        campoNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        campoMatricula = new javax.swing.JTextField();
        campoBloco = new javax.swing.JTextField();
        campoSala = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        jLabel4.setText("jLabel4");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cadastro - Biblioteca");
        setResizable(false);

        jLabel1.setText("Nome Completo");
        
        campoNome.addFocusListener(new java.awt.event.FocusAdapter() {
        	/**
        	 * Evento acionado pela iniciativa do usuario de deixar o campo nome pronto para uso.
        	 */
            public void focusLost(java.awt.event.FocusEvent evt) {
                campoNomeFocusLost(evt);
            }
        });
        
        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					botaoSalvarActionPerformed(evt);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        
        campoMatricula.addFocusListener(new java.awt.event.FocusAdapter() {
        	/**
        	 * Evento acionado pela iniciativa do usuario de deixar o campo matricula pronto para uso.
        	 */
        	public void focusLost(java.awt.event.FocusEvent evt) {
        		campoMatriculaFocusLost(evt);
        	}
        });
        
        campoBloco.addFocusListener(new java.awt.event.FocusAdapter() {
        	/**
        	 * Evento acionado pela iniciativa do usuario de deixar o campo bloco pronto para uso.
        	 */
        	public void focusLost(java.awt.event.FocusEvent evt) {
        		campoBlocoFocusLost(evt);
        	}
        });
        
        campoSala.addFocusListener(new java.awt.event.FocusAdapter(){
        	/**
        	 * Evento acionado pela iniciativa do usuario de deixar o campo sala pronto para uso.
        	 */
        	public void focusLost(java.awt.event.FocusEvent evt) {
        		campoSalaFocusLost(evt);
        	}
        });
        

        jLabel2.setText("Bloco");

        jLabel3.setText("Sala");

        jLabel5.setText("Matricula");

        jButton2.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(49, 49, 49))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campoBloco, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campoSala, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(38, 38, 38))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(29, 29, 29)))
                        .addComponent(jButton2)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoBloco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoSala, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoSalvar)
                    .addComponent(jButton2))
                .addGap(25, 25, 25))
        );

        pack();
    }

    
    /**
     * Configura o que o usuario digitou para o campo nome.
     * @param evt
     * 		Parametro a ser configurado para o campo nome.
     */
	private void campoNomeFocusLost(java.awt.event.FocusEvent evt) {
    	Biblioteca.setTxtNome(campoNome.getText());
        
    }
    
	/**
     * Configura o que o usuario digitou para o campo matricula.
     * @param evt
     * 		Parametro a ser configurado para o campo matricula.
     */
	private void campoMatriculaFocusLost(java.awt.event.FocusEvent evt) {
		// TODO Auto-generated method stub
		Biblioteca.setTxtMatricula(campoMatricula.getText());
		
	}
	
	/**
     * Configura o que o usuario digitou para o campo bloco.
     * @param evt
     * 		Parametro a ser configurado para o campo bloco.
     */
	private void campoBlocoFocusLost(FocusEvent evt) {
		// TODO Auto-generated method stub
		Biblioteca.setTxtBloco(campoBloco.getText());
		
		
	}
	
	/**
     * Configura o que o usuario digitou para o campo sala.
     * @param evt
     * 		Parametro a ser configurado para o campo sala.
     */
    private void campoSalaFocusLost(FocusEvent evt) {
		// TODO Auto-generated method stub
		Biblioteca.setTxtSala(campoSala.getText());
		
	}

    /**
     * Salva os dados que o usuario digitou nos campos da tela de cadastro.
     * @param evt
     * 		Parametro que serve de controle para manusear os metodos da biblioteca.
     * @throws Exception
     * 		Lanca excecao, caso o usuario digite alguma campo de maneira errada.
     */
    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) throws Exception{
    	
    	if (campoNome.getText().length() > 0 && campoBloco.getText().length() > 0 && campoMatricula.getText().length() > 0 && campoSala.getText().length() > 0 ) {
        	//original
        	try {
        		sala = new Sala(Biblioteca.getTxtBloco(), Biblioteca.getNumeroDaSala());
    		} catch (Exception e) {
    			JOptionPane.showMessageDialog(null, e.getMessage());
    		}
        	 	
        	try {
        		professor = new Professor(Biblioteca.getTxtNome(), Biblioteca.getTxtMatricula(), sala);
    		} catch (Exception e) {
    			JOptionPane.showMessageDialog(null, e.getMessage());
    		}	
            	
            Biblioteca.adicionaProfessor(professor);

            String login = JOptionPane.showInputDialog("Digite o Login:");
            String senha = JOptionPane.showInputDialog("Digite a senha:");
            professor.setLogin(login, senha);
           
            Biblioteca.telaDeLogin.setVisible(true);
        	Biblioteca.telaDeCadastro.dispose();
            professor.setTelaDeUsuario(new TelaDeUsuario(professor));
            professor.telaDeUsuario.setLocationRelativeTo(null);
            professor.telaDeUsuario.setVisible(true);
            Biblioteca.telaDeLogin.dispose();
		}
    	else {
			JOptionPane.showMessageDialog(null, "Preencha todos os campos");
			Biblioteca.telaDeCadastro.dispose();
			Biblioteca.telaDeCadastro.setVisible(true);
		}

        
    }
    

}